//
//  InpCostsDetailVC.m
//  掌上宏力
//
//  Created by admin on 2017/5/16.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "InpCostsDetailVC.h"
#import "InpCostCell.h"
#import "InpCostsModel.h"
#import "InCostsViewModel.h"

@interface InpCostsDetailVC ()

@property (nonatomic,strong) InCostsViewModel *viewModel;
@property (nonatomic,strong) NSMutableArray *datasArray;

@end

@implementation InpCostsDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"住院费用明细";
}

-(void)getDatasArray{
    [self getDatasArray:^(NSString *page, GetDatas handler) {
        _viewModel = [[InCostsViewModel alloc] init];
        NSString *p = [NSString stringWithFormat:@"%ld",self.currentPage];
        [_viewModel getInCostsInfoWithPage:p success:^{
            handler(_viewModel.datasArray);
            if (self.currentPage == 1) {
                _datasArray = [NSMutableArray arrayWithArray:_viewModel.datasArray];
            }else{
                [_datasArray addObjectsFromArray:_viewModel.datasArray];
            }
            handler(_viewModel.datasArray);
        }];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _datasArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    InpCostsModel *model = _datasArray[section];
    NSString *title = model.date;
    return [title substringToIndex:title.length-8];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InpCostCell *cell = [InpCostCell cellWithTableView:tableView];
    cell.costInfo = _datasArray[indexPath.section];
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == _datasArray.count - 1) {
        self.currentPage ++;
        [self getDatasArray];
    }
}
@end
